package com.ht.redis.controller;

import com.alibaba.fastjson.JSON;
import com.ht.Result;
import com.ht.redis.form.RedisConfigForm;
import com.ht.redis.service.RedisConnectionService;
import com.ht.redis.vo.RedisView;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@CrossOrigin
@RestController
@RequestMapping("/api/redis")
public class RedisController {
    // @Autowired
    // private StringRedisTemplate stringRedisTemplate;

    @Resource
    private RedisTemplate stringRedisTemplate;

    @GetMapping("getKeyPage")
    public Result<List<RedisView>> getKeyPage(@RequestParam(value = "keyword") String keyword){
        long start = System.currentTimeMillis();
        List<RedisView> list = new ArrayList<>();
        Set<String> keys = stringRedisTemplate.keys("*" + keyword + "*");
        for (String key : keys) {
            RedisView redisView = new RedisView();
            redisView.setKeyName(key);
            DataType type = stringRedisTemplate.type(key);
            redisView.setDataType(type.code());
            redisView.setTtl(stringRedisTemplate.getExpire(key, TimeUnit.SECONDS));
            list.add(redisView);
        }
        long end = System.currentTimeMillis();
        System.out.println("耗时："+(end-start));
        return Result.success(list);
    }



    @GetMapping("getKey")
    public Result<RedisView> getValue(@RequestParam("key") String key){
        long start = System.currentTimeMillis();
        RedisView redisView = new RedisView();
        redisView.setKeyName(key);

        Long expire = stringRedisTemplate.getExpire(key, TimeUnit.SECONDS);
        redisView.setTtl(expire);

        DataType type = stringRedisTemplate.type(key);
        redisView.setDataType(type.code());
        if (DataType.STRING.equals(type)){
            Object value = stringRedisTemplate.opsForValue().get(key);
            redisView.setValue(value);
        } else if (DataType.HASH.equals(type)){
            String value = JSON.toJSONString(stringRedisTemplate.opsForHash().entries(key));
            redisView.setValue(value);
        }

        long end = System.currentTimeMillis();
        System.out.println("耗时："+(end-start));
        return Result.success(redisView);
    }

    @GetMapping("create")
    public Result<String> create(@RequestParam("key") String key){
        User user = new User();
        user.setName("张三");
        user.setAge(18);
        stringRedisTemplate.opsForHash().put(key, "test1", JSON.toJSONString(user));
        return Result.success();
    }
    @Data
    class User {
        private String name;
        private Integer age;
    }


    @Autowired
    private RedisConnectionService redisService;

    @PostMapping("/config")
    public Result<String> updateRedisConfig(@RequestBody RedisConfigForm config) {
        redisService.updateRedisConnection(config.getHost(), config.getPort(), config.getPassword());
        return Result.success("Redis配置更新成功");
    }

}